#ifndef HFT_EXECUTION_TYPES_H
#define HFT_EXECUTION_TYPES_H

#include <string>
#include <vector>
#include <chrono>
#include <unordered_map>

namespace hft {

enum class OrderDirection {
    BUY,
    SELL
};

enum class ExecutionStatus {
    PENDING,
    SUBMITTED,
    PARTIALLY_FILLED,
    FILLED,
    CANCELLED,
    REJECTED
};

enum class OrderStatus {
    Created,
    Pending,
    PartialFilled,
    Filled,
    Canceled,
    Rejected
};

struct ExecutionResult {
    std::string order_id;
    std::string symbol;
    OrderDirection direction;
    double price;
    double volume;
    ExecutionStatus status;
    std::string message;
    int64_t timestamp;
};

struct Position {
    std::string symbol;
    std::string account;
    std::string strategy_id;
    
    // 持仓数据
    int history_volume = 0;
    double history_avg_price = 0.0;
    int today_volume = 0;
    double today_avg_price = 0.0;
    
    // 开平仓统计
    int open_volume = 0;
    double open_amount = 0.0;
    int close_volume = 0;
    double close_amount = 0.0;
    
    // 盈亏数据
    double realized_pnl = 0.0;
    double unrealized_pnl = 0.0;
    double last_price = 0.0;
    double margin_ratio = 0.0;
    
    // 时间戳
    std::chrono::system_clock::time_point create_time;
    std::chrono::system_clock::time_point update_time;
    
    void updatePosition(const Trade& trade);
    void updateLastPrice(double price);
    void updateMargin(double margin_ratio);
};

struct Account {
    double balance;
    double available;
    double margin;
    double margin_rate;
};

struct Order {
    std::string order_id;
    std::string symbol;
    std::string account;
    std::string strategy_id;
    bool is_buy;
    double price;
    int volume;
    int filled_volume = 0;
    double avg_fill_price = 0.0;
    OrderStatus status = OrderStatus::Created;
    std::chrono::system_clock::time_point create_time;
    std::chrono::system_clock::time_point update_time;
};

struct Trade {
    std::string trade_id;
    std::string order_id;
    std::string symbol;
    std::string account;
    std::string strategy_id;
    bool is_buy;
    double price;
    int volume;
    std::chrono::system_clock::time_point trade_time;
};

} // namespace hft

#endif // HFT_EXECUTION_TYPES_H